/**
 * Created by Boyz.X on 2015/9/21.
 */
appModule.controller('followDetialCtr', function ($scope, $state, CommonUtils, $http, $stateParams, $ionicActionSheet,$ionicScrollDelegate) {
  var cid = eval("(" + $stateParams.cid + ")");
  $scope.cname = eval("(" + $stateParams.cname + ")");
  //TODO 获取用户信息
  $scope.userid = 'sie_caobinglian';
  $scope.textmessage = '';
  $scope.isRight = function (userid) {
    return $scope.userid == userid;
  };
  $http.post(webserver + 'com.chinasie.s2c.users.users.querymessagebyCID.biz.ext', {
    cid: cid
  }).success(function (data) {
    $scope.messages = [];
    angular.forEach(data.messages, function (message, index) {
      if(message.textRecord != null) {
        message.record='<p>' + message.textRecord + '</p>';
      }else if(message.imgRecord != null){
        message.record = '<img src="' + message.imgRecord +'"/>';
      }
      $scope.messages.push(message);
    });
    //$scope.messages = data.messages;
    $ionicScrollDelegate.scrollBottom();
  });

  $scope.Detial = function () {
    $state.go('followDetial');
  };
  $scope.back = function () {
    CommonUtils.goBackTo('tab.follow');
  };
  $scope.voice = function () {
    wpt.takeVoice(
      '',
      function (data) {
        var voicedata = eval(data);
        alert(data);
        alert(JSON.stringify(data));
        $http
      },
      function (err) {
        alert(JSON.stringify(err))
      }
    );
  };

  /*  var messageOptions = [
   { content: '<p>Wow, this is really something huh?</p>' },
   { content: '<p>Yea, it\'s pretty sweet</p>' },
   { content: '<p>I think I like Ionic more than I like ice cream!</p>' },
   { content: '<p>Gee wiz, this is something special.</p>' },
   { content: '<p>Am I dreaming?</p>' },
   { content: '<p>Yea, it\'s pretty sweet</p>' },
   { content: '<p>I think I like Ionic more than I like ice cream!</p>' }
   ];*/

  var messageIter = 0;
  //$scope.messages = messageOptions.slice(0, messageOptions.length);

  $scope.add = function () {
    var nextMessage = messageOptions[messageIter++ % messageOptions.length];
    $scope.messages.push(angular.extend({}, nextMessage));

    // Update the scroll area and tell the frosted glass to redraw itself
    $ionicFrostedDelegate.update();
    $ionicScrollDelegate.scrollBottom(true);
  };
  $scope.sendmessage = function (data) {

    $http.post(webserver + 'com.chinasie.s2c.users.users.sendmessage.biz.ext', {
      messagetype: "text", cid: cid, message: data, userid: $scope.userid
    }).success(function (data) {
      $scope.textmessage = null;
      $http.post(webserver + 'com.chinasie.s2c.users.users.querymessagebyCID.biz.ext', {
        cid: cid
      }).success(function (data) {
        $scope.messages = [];
        angular.forEach(data.messages, function (message, index) {
          if(message.textRecord != null) {
            message.record='<p>' + message.textRecord + '</p>';
          }else if(message.imgRecord != null){
            message.record = '<img src="' + message.imgRecord +'"/>';
          }
          $scope.messages.push(message);
        });
        //$scope.messages = data.messages;
      });

    });
  };
  $scope.uploadimg = function () {
    $ionicActionSheet.show({
      titleText: '分享照片',
      buttons: [{
        text: '<i class="icon ion-share"></i> 拍照上传'
      }, {
        text: '<i class="icon ion-arrow-move"></i> 相册上传'
      }],
      cancelText: 'Cancel',
      cancel: function () {
        console.log('CANCELLED');
      },
      buttonClicked: function (index) {
        console.log('BUTTON CLICKED', index);
        if (index == '0') {
          /*          wpt.takePhotoAndUpload(
           '',//传入jsonobject格式的参数，没有参数传’’字符
           function (data){
           alert(JSON.stringify(data));
           $http.post(webserver + 'com.chinasie.s2c.users.users.sendmessage.biz.ext', {
           messagetype:"img" , cid:cid , message:data.serverPath , userid:$scope.userid
           }).success(function (data) {
           alert(JSON.stringify(data));
           });
           },
           function(data){
           alert(data);
           }
           );*/

          wpt.takePhoto(
            '',//传入jsonobject格式的参数，没有参数传’’字符
            function (data) {
              onedata = eval("(" + data + ")");
              var params = [
                {
                  'filePath': onedata.filePath,
                  'fileSize': '256k',
                  'fileName': 'test.jpg'
                }];
              wpt.uploadFile(
                angular.toJson(params)
                ,//传入jsonobject格式的参数，没有参数传’’字符
                function (data) {
                  data = eval("(" + data + ")");
                    $http.post(webserver + 'com.chinasie.s2c.users.users.sendmessage.biz.ext', {
                      messagetype: "img", cid: cid, message: data[0].serverPath, userid: $scope.userid
                    }).success(function (data) {
                      $http.post(webserver + 'com.chinasie.s2c.users.users.querymessagebyCID.biz.ext', {
                        cid: cid
                      }).success(function (data) {
                        $scope.messages = [];
                        angular.forEach(data.messages, function (message, index) {
                          if(message.textRecord != null) {
                            message.record='<p>' + message.textRecord + '</p>';
                          }else if(message.imgRecord != null){
                            message.record = '<img src="' + message.imgRecord +'"/>';
                          }
                          $scope.messages.push(message);
                        });
                        //$scope.messages = data.messages;
                      });
                    });
                },
                function (data) {
                  alert(JSON.stringify(data));
                }
              )
            },
            function (data) {
              alert(JSON.stringify(data))
            }
          );
        } else if (index == '1') {
          var params = [];
          wpt.selectPhoto(
            '',//传入jsonobject格式的参数，没有参数传’’字符
            function (data) {
              onedata = eval("(" + data + ")");
              angular.forEach(onedata,function(item,index){
                params.push({
                  'filePath': item.filePath,
                  'fileSize': '256k',
                  'fileName': 'test.jpg'})
              });
              wpt.uploadFile(
                angular.toJson(params)
                ,//传入jsonobject格式的参数，没有参数传’’字符
                function (data) {
                  data = eval("(" + data + ")");
                  $http.post(webserver + 'com.chinasie.s2c.users.users.sendmessage.biz.ext', {
                    messagetype: "img", cid: cid, message: data[0].serverPath, userid: $scope.userid
                  }).success(function (data) {
                    $http.post(webserver + 'com.chinasie.s2c.users.users.querymessagebyCID.biz.ext', {
                      cid: cid
                    }).success(function (data) {
                      $scope.messages = [];
                      angular.forEach(data.messages, function (message, index) {
                        if(message.textRecord != null) {
                          message.record='<p>' + message.textRecord + '</p>';
                        }else if(message.imgRecord != null){
                          message.record = '<img src="' + message.imgRecord +'"/>';
                        }
                        $scope.messages.push(message);
                      });
                      //$scope.messages = data.messages;
                    });
                  });
                },
                function (data) {
                  alert(JSON.stringify(data));
                }
              )
            },
            function (data) {
              alert(JSON.stringify(data))
            }
          );
        }
        return true;
      },
      destructiveButtonClicked: function () {
        console.log('DESTRUCT');
        return true;
      }
    });
  }
});


